package com.me.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.me.bean.AjaxResp;
import com.me.entity.Friends;
import com.me.entity.RecentChat;
import com.me.service.FriendService;
import com.me.service.RecentChatService;
import com.me.service.UserInfoService;
@Controller
public class FriendsController {
	
	@Autowired
	FriendService friendService;
	
	@Autowired
	UserInfoService userInfoService;
	@Autowired
	RecentChatService recentChatService;
	
	@RequestMapping("/getFriList_contact")
	@ResponseBody
	public AjaxResp getAllFriends(String account){
		AjaxResp ar=new AjaxResp();
		ar.setSuccess(true);
		List list=friendService.getAllFriendsByAccount(account);
		ar.setData(list);
		return ar;
	}
	
	@RequestMapping("delete_contact")
	@ResponseBody
	@Transactional
	public AjaxResp deleteFriend(String account,String friaccount){
		AjaxResp ar=new AjaxResp();
		ar.setSuccess(false);
		int uid=userInfoService.getUserIdByAccount(account);
		int fid=userInfoService.getUserIdByAccount(friaccount);
		
		List list=friendService.getFriendsEntity(uid, fid);
		if(list.size()==0 || list.isEmpty()){
			return ar;
		}
		Friends f=(Friends)list.get(0);
		friendService.delete(f);
		
		//删除最近联系人
		
		List list_=recentChatService.getRecent(uid, fid, "user");
		if(list_.size()==0 || list.isEmpty()){
			ar.setSuccess(true);
			return ar;
		}
		
		for(int i=0;i<list_.size();i++){
			recentChatService.delete((RecentChat)list_.get(i));
		}
		
		
		
		
		ar.setSuccess(true);
		return ar;
	}
	
}
